package com.trailer.base.net

import com.trailer.base.net.BOkhttpClient.client
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.*

/**
 * Created by xianguangjin on 15/12/14.
 */
object BNetEngine {

    private val map = WeakHashMap<String, Retrofit>()

    fun retrofit(url: String): Retrofit {
        return if (map.containsKey(url)) {
            map[url] ?: initRetrofit(url)
        } else {
            initRetrofit(url)
        }
    }

    private fun initRetrofit(url: String): Retrofit {
        val retrofit = Retrofit.Builder()
                .baseUrl(url)
                .client(client()) //                    .addConverterFactory(FastJsonConverterFactory.create())
                //                    .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
        map[url] = retrofit
        return retrofit
    }


}
